package com.txunda.zhuohired.aty.user.resume;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.JSONUtils;
import com.ants.theantsgo.util.L;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.BottomDialog;
import com.smarttop.library.widget.OnAddressSelectedListener;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.aty.user.home.ExpectJobAty;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.manage.IndustryBean;
import com.txunda.zhuohired.bean.user.resume.ExpectPayBean;
import com.txunda.zhuohired.bean.user.resume.JobStatusBean;
import com.txunda.zhuohired.bean.user.resume.PurposeBean;
import com.txunda.zhuohired.dialog.ExpectPayDialog;
import com.txunda.zhuohired.dialog.IndustryDialog;
import com.txunda.zhuohired.dialog.JobStatusDialog;
import com.txunda.zhuohired.http.pst.MemberPst;
import com.txunda.zhuohired.http.pst.ResumePst;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/1 11:00
 * 功能描述：管理求职意向
 * 联系方式：jiahuanchen@126.com
 */

public class PurposeAty extends BaseAty implements AddressSelector.onSelectorAreaPositionListener,
        OnAddressSelectedListener, AddressSelector.OnDialogCloseListener {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 保存
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * 期望工作地
     */
    @ViewInject(R.id.tv_job_purpose_address)
    private TextView tv_job_purpose_address;
    /**
     * 期望行业
     */
    @ViewInject(R.id.tv_job_purpose_industry)
    private TextView tv_job_purpose_industry;
    /**
     * 期望职位
     */
    @ViewInject(R.id.tv_job_purpose_job)
    private TextView tv_job_purpose_job;
    /**
     * 期望薪资
     */
    @ViewInject(R.id.tv_job_purpose_pay)
    private TextView tv_job_purpose_pay;
    /**
     * 职位状态
     */
    @ViewInject(R.id.tv_job_purpose_status)
    private TextView tv_job_purpose_status;

    private int REQUESTEXPECTCODE = 1;//期望职位
    private BottomDialog dialog;
    private int provincePosition;
    private int cityPosition;
    private int countyPosition;
    private int streetPosition;
    private String provinceCode = "";
    private String cityCode = "";
    private String countyCode = "";
    private String streetCode = "";
    private List<IndustryBean.DataBean> industryList;
    private MemberPst memberPst;
    private ResumePst resumePst;
    private String token = "";
    private List<JobStatusBean.DataBean> jonStatusList;
    private String job_status_id = "";
    private List<ExpectPayBean.DataBean> expectPayList;
    private String expect_pay_id = "";
    private String job_id = "";//职位id
    private String trade_id = "";//行业id

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("求职意向");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("保存");
    }

    @Override
    @OnClick({R.id.tv_layout_white_right, R.id.ll_job_purpose_address, R.id.ll_job_purpose_industry, R.id
            .ll_job_purpose_job, R.id.ll_job_purpose_pay, R.id.ll_job_purpose_status})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_layout_white_right://保存
                String address = tv_job_purpose_address.getText().toString().trim();//期望工作地
                String industry = tv_job_purpose_industry.getText().toString().trim();//期望行业
                String job = tv_job_purpose_job.getText().toString().trim();//期望工作
                String price = tv_job_purpose_pay.getText().toString().trim();//期望薪资
                String job_status = tv_job_purpose_status.getText().toString().trim();//求职状态
                if (TextUtils.isEmpty(address)) {
                    showErrorTip("请选择期望工作地");
                    return;
                }
                if (TextUtils.isEmpty(industry)) {
                    showErrorTip("请选择期望行业");
                    return;
                }
                if (TextUtils.isEmpty(job)) {
                    showErrorTip("请选择期望职位");
                    return;
                }
                if (TextUtils.isEmpty(price)) {
                    showErrorTip("请选择期望薪资");
                    return;
                }
                if ("面议".equals(price)) {
                    expect_pay_id = "1";
                }
                if (TextUtils.isEmpty(job_status)) {
                    showErrorTip("请选择求职状态");
                    return;
                }
                L.e("======provinceCode====", provinceCode);
                L.e("======cityCode====", cityCode);
                L.e("======countyCode====", countyCode);
                L.e("======trade_id====", trade_id);
                L.e("======job_id====", job_id);
                L.e("======expect_pay_id====", expect_pay_id);
                L.e("======job_status_id====", job_status_id);
                memberPst.perfectTwoIn(provinceCode, cityCode, countyCode, trade_id, job_id, expect_pay_id,
                        job_status_id, token);
                break;
            case R.id.ll_job_purpose_address://期望工作地
                if (dialog != null) {
                    dialog.show();
                } else {
                    dialog = new BottomDialog(this);
                    dialog.setOnAddressSelectedListener(this);
                    dialog.setDialogDismisListener(this);
                    dialog.setTextSize(14);//设置字体的大小
                    dialog.setIndicatorBackgroundColor(android.R.color.holo_orange_light);//设置指示器的颜色
                    dialog.setTextSelectedColor(android.R.color.holo_orange_light);//设置字体获得焦点的颜色
                    dialog.setTextUnSelectedColor(android.R.color.holo_blue_light);//设置字体没有获得焦点的颜色
                    //            dialog.setDisplaySelectorArea("31",1,"2704",1,"2711",0,"15582",1);//设置已选中的地区
                    dialog.setSelectorAreaPositionListener(this);
                    dialog.show();
                }
                break;
            case R.id.ll_job_purpose_industry://期望行业
                IndustryDialog industryDialog = new IndustryDialog(this, industryList, new IndustryDialog
                        .SelectStationListener() {
                    @Override
                    public void onSelectStation(String type, String industry_id) {
                        tv_job_purpose_industry.setText(type);
                        trade_id = industry_id;
                    }
                });
                industryDialog.show();
                break;
            case R.id.ll_job_purpose_job://期望职位
                startActivityForResult(ExpectJobAty.class, null, REQUESTEXPECTCODE);
                break;
            case R.id.ll_job_purpose_pay://期望薪资
                ExpectPayDialog expectPayDialog = new ExpectPayDialog(this, expectPayList, new ExpectPayDialog
                        .ExpectPayListener() {
                    @Override
                    public void onExpectPay(String type, String id) {
                        tv_job_purpose_pay.setText(type);
                        expect_pay_id = id;
                    }
                });
                expectPayDialog.show();
                break;
            case R.id.ll_job_purpose_status://职位状态
                JobStatusDialog jobStatusDialog = new JobStatusDialog(this, jonStatusList, new JobStatusDialog
                        .JobStatusListener() {
                    @Override
                    public void onJobStatus(String type, String industry_id) {
                        tv_job_purpose_status.setText(type);
                        job_status_id = industry_id;
                    }
                });
                jobStatusDialog.show();
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_job_purpose;
    }

    @Override
    protected void initialized() {
        industryList = new ArrayList<>();
        memberPst = new MemberPst(this);
        resumePst = new ResumePst(this);
        token = application.getUserInfo().get("data");
        jonStatusList = new ArrayList<>();
        expectPayList = new ArrayList<>();
    }

    @Override
    protected void requestData() {
        memberPst.trade();//行业
        memberPst.service();//求职状态
        memberPst.salary();//期望薪资
        resumePst.jobIntension(token);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (RESULT_OK == resultCode) {
            if (requestCode == REQUESTEXPECTCODE) {//期望职位
                ArrayList<String> expect_job_name = (ArrayList<String>) data.getSerializableExtra("expect_job_name");
                ArrayList<String> expect_job_id = (ArrayList<String>) data.getSerializableExtra("expect_job_id");
                String name = expect_job_name.toString();
                tv_job_purpose_job.setText(name.substring(1, name.length() - 1));
                job_id = expect_job_id.toString().substring(1, expect_job_id.toString().length() - 1);
                L.e("=====job_id=====", job_id);
            }
        }
    }

    @Override
    public void selectorAreaPosition(int provincePosition, int cityPosition, int countyPosition, int streetPosition) {
        this.provincePosition = provincePosition;
        this.cityPosition = cityPosition;
        this.countyPosition = countyPosition;
        this.streetPosition = streetPosition;
    }

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        provinceCode = (province == null ? "" : province.code);
        cityCode = (city == null ? "" : city.code);
        countyCode = (county == null ? "" : county.code);
        streetCode = (street == null ? "" : street.code);
        String s = (province == null ? "" : province.name) + "-" + (city == null ? "" : city.name) + "-" + (county ==
                null ? "" : county.name);
        L.e("=================", s);
        tv_job_purpose_address.setText(s);
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void dialogclose() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("trade")) {//行业
            //            L.e("======trade====", jsonStr);
            IndustryBean industryBean = GsonUtil.GsonToBean(jsonStr, IndustryBean.class);
            industryList = industryBean.getData();
            return;
        }
        if (requestUrl.contains("service")) {//求职状态
            //            L.e("======service====", jsonStr);
            JobStatusBean jobStatusBean = GsonUtil.GsonToBean(jsonStr, JobStatusBean.class);
            jonStatusList = jobStatusBean.getData();
            return;
        }
        if (requestUrl.contains("salary")) {//期望薪资
            //            L.e("======salary====", jsonStr);
            ExpectPayBean expectPayBean = GsonUtil.GsonToBean(jsonStr, ExpectPayBean.class);
            expectPayList = expectPayBean.getData();
            return;
        }
        if (requestUrl.contains("jobIntension")) {
            //            L.e("==========jobIntension==========", jsonStr);
            PurposeBean purposeBean = GsonUtil.GsonToBean(jsonStr, PurposeBean.class);
            if (TextUtils.isEmpty(purposeBean.getData().getWorkplace_province()) && TextUtils.isEmpty(purposeBean
                    .getData().getWorkplace_city()) && TextUtils.isEmpty(purposeBean.getData().getWorkplace_area())) {
                tv_job_purpose_address.setHint("请选择期望工作地");
            } else {
                tv_job_purpose_address.setText(purposeBean.getData().getWorkplace_province() + "-" + purposeBean
                        .getData().getWorkplace_city() + "-" + purposeBean.getData().getWorkplace_area());
            }
            if (TextUtils.isEmpty(purposeBean.getData().getTrade())) {
                tv_job_purpose_industry.setHint("请选择期望行业");
            } else {
                tv_job_purpose_industry.setText(purposeBean.getData().getTrade());
            }
            if (TextUtils.isEmpty(purposeBean.getData().getPosition())) {
                tv_job_purpose_job.setHint("请选择期望职位");
            } else {
                tv_job_purpose_job.setText(purposeBean.getData().getPosition());
            }
            if (TextUtils.isEmpty(purposeBean.getData().getSalary())) {
                tv_job_purpose_pay.setHint("请选择期望薪资");
            } else {
                tv_job_purpose_pay.setText(purposeBean.getData().getSalary());
            }
            if (TextUtils.isEmpty(purposeBean.getData().getService())) {
                tv_job_purpose_status.setHint("请选择求职状态");
            } else {
                tv_job_purpose_status.setText(purposeBean.getData().getService());
            }
            job_status_id = purposeBean.getData().getSalary_id();
            job_id = purposeBean.getData().getPosition_id();
            trade_id = purposeBean.getData().getTrade_id();
            return;
        }
        if (requestUrl.contains("perfectTwoIn")) {//完善简历第二页提交
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            this.finish();
            return;
        }
    }
}
